####################################################################################################
### Replication code for Figure 1 in
### Höhmann D. & Tober, T. 'Electoral Rules and Partisan Control of Government: A Replication Study'
### The Journal of Politics
####################################################################################################

# Code adapted from: https://www.cambridge.org/core/journals/perspectives-on-politics/article/using-graphs-instead-of-tables-in-political-science/9FD63E9EE686AF046732191EE8A68034

rm(list = ls())

# Get cell counts
prop.left <- 253
prop.right <- 158
maj.left <- 162
maj.right <- 180
 
prop.left.gov <- 253
prop.right.gov <- 158
maj.left.gov <- 203
maj.right.gov <- 199

# Create matrix for stacked barplot (since using horizontal barplot, need to invert matrices and labels)
year.matrix <- 100*cbind(c(maj.left/(maj.left+maj.right), maj.right/(maj.left+maj.right)),
                         c(prop.left/(prop.left+prop.right), prop.right/(prop.left+prop.right)))
country.matrix <- 100*cbind(c(maj.left.gov/(maj.left.gov+maj.right.gov), maj.right.gov/(maj.left.gov+maj.right.gov)),
                            c(prop.left.gov/(prop.left.gov+prop.right.gov), prop.right.gov/(prop.left.gov+prop.right.gov)))


####################################################################################################
### Mosaic plot
####################################################################################################

# Left plot

prop.vec.mosaic <- c(prop.left, prop.right, maj.left, maj.right)
prop.matrix.mosaic <- matrix(prop.vec.mosaic, nrow = 2, ncol =2, byrow = T,
                             dimnames = list(c("Proportional", "Majoritarian"), c("Left", "Right")))
prop.table.mosaic  <- as.table(prop.matrix.mosaic)

pdf("figure1.pdf", width = 8.5, height = 5.7) 
par(mfrow = c(1,2), mar=c(.5,3,6.6,1.2))

mosaicplot(prop.table.mosaic, color = c("gray60", "gray80"), 
           main = "", shade = FALSE, xlab = "", 
           ylab = "", las = 1, cex.axis =.8)
text(.37,.18,"158") 
text(.37,.13,"(38.4%)")
text(.37,.7,"253")
text(.37,.65,"(61.6%)")
text(.8,.27,"180")
text(.8,.22,"(52.6%)")
text(.8,.77,"162")
text(.8,.72,"(47.4%)")
mtext("Without additional observations \nfor USA and Canada (N=753)", 3, line = 3, 
      cex = 1.2, font = 2) 
mtext("Government Partisanship", 2, font = 2, line = 1, cex = 1.1)
mtext("Electoral System", 3, font = 2, line = 1, cex = 1.1)

# Right plot

prop.vec.mosaic.gov <- c(prop.left.gov, prop.right.gov, maj.left.gov, maj.right.gov)
prop.matrix.mosaic.gov <- matrix(prop.vec.mosaic.gov, nrow = 2, ncol =2, byrow = T,
                                 dimnames = list(c("Proportional", "Majoritarian"), c("", "")))
prop.table.mosaic.gov  <- as.table(prop.matrix.mosaic.gov)

mosaicplot(prop.table.mosaic.gov, color = c("gray60", "gray80"), 
           main = "", shade = FALSE, xlab = "", 
           ylab = "", las = 1, cex.axis =.8)
text(.25,.18,"158") 
text(.25,.13,"(38.4%)")
text(.25,.7,"253")
text(.25,.65,"(61.6%)")
text(.75,.23,"199")
text(.75,.18,"(49.5%)")
text(.75,.77,"203")
text(.75,.72,"(50.5%)")
mtext("With additional oberservations \nfor USA and Canada (N=813)", 3, line = 3, 
      cex = 1.2, font = 2) 
mtext("Electoral System", 3, font = 2, line = 1, cex  = 1.1)

dev.off() 
